package com.example.springtxjms.web;

import com.example.springtxjms.service.CustomerService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CustomerResource {
    @Autowired
    private CustomerService customerService;
    @Autowired
    private JmsTemplate jmsTemplate;

    @PostMapping("/msg/listen")
    public void create(@RequestParam String msg) {
        jmsTemplate.convertAndSend(CustomerService.NEW_MSG, msg);
    }

    @PostMapping("/msg/direct")
    public void handle(@RequestParam String msg) {
        customerService.handle(msg);
    }

    @GetMapping("/read")
    public String read() {
        Object o = jmsTemplate.receiveAndConvert(CustomerService.REPLY_MGS);
        return String.valueOf(o);
    }
}
